﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.Model;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class CustomerFeedbackService : RepositoryBase
    {
        public List<CustomerFeedbackMode> GetCustomerFeedback()
        {
            List<CustomerFeedbackMode> models = new List<CustomerFeedbackMode>();
            IQueryable<CustomerFeedback> customerFeedbackEntities = GetEntities<CustomerFeedback>().OrderByDescending(c => c.hfTime);
            foreach (CustomerFeedback entity in customerFeedbackEntities)
            {
                
                ApplicationProduct product = GetEntities<ApplicationProduct>().Where(p => p.WXNO.Equals(entity.WXNO)).FirstOrDefault();
                if (product != null)
                {
                    CustomerFeedbackMode model = new CustomerFeedbackMode();
                    if (!product.ApplicationFormReference.IsLoaded)
                    {
                        product.ApplicationFormReference.Load();
                    }
                    if (!product.ApplicationForm.UserReference.IsLoaded)
                    {
                        product.ApplicationForm.UserReference.Load();
                    }
                    model.CustomerName = product.ApplicationForm.User.FullName;
                    model.HFDate = entity.hfTime;
                    model.ProductID = entity.ApplicationProductID;
                    model.FormID = entity.ApplicationFormID;
                    model.HFFrom = entity.hfFrom;
                    model.hfResult = entity.hfResult;
                    model.InfoState = entity.InfoState;
                    model.khAdvice = entity.khAdvice;
                    model.khRequire = entity.khRequire;
                    model.myd = entity.myd;
                    model.RMANO = product.ApplicationForm.ApplicationCode;
                    model.SerialNO = product.ProductSerialNumber;
                    model.ID = entity.ID.ToString();
                    models.Add(model);
                }
            }
            return models;
        }

        public CustomerFeedbackMode GetCustomerFeedback(long id)
        {
            CustomerFeedback entity = GetEntities<CustomerFeedback>().Where(c => c.ID == id).FirstOrDefault();
            CustomerFeedbackMode model = new CustomerFeedbackMode();
            ApplicationProduct product = GetEntities<ApplicationProduct>().Where(p => p.WXNO.Equals(entity.WXNO)).FirstOrDefault();
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            model.CustomerName = product.ApplicationForm.User.FullName;
            model.HFDate = entity.hfTime;
            model.ProductID = entity.ApplicationProductID;
            model.FormID = entity.ApplicationFormID;
            model.HFFrom = entity.hfFrom;
            model.hfResult = entity.hfResult;
            model.InfoState = entity.InfoState;
            model.khAdvice = entity.khAdvice;
            model.khRequire = entity.khRequire;
            model.myd = entity.myd;
            model.RMANO = product.ApplicationForm.ApplicationCode;
            model.SerialNO = product.ProductSerialNumber;
            model.ID = entity.ID.ToString();
            return model;
        }
    }
}
